
package urnoted;

import java.awt.*;
import javax.swing.*;

import urnoted.Controller;

public class ResultsPanel extends JPanel {
    private static final int WIDTH = 500;
    private static final int HEIGHT = 20;
    private JLabel label;
    private int score, total;
    private Controller controller;

    public ResultsPanel(Controller controller) {
        this.controller = controller;
        this.setBorder(BorderFactory.createTitledBorder("Results"));
        JPanel labelPanel = new JPanel();
        labelPanel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
        labelPanel.setLayout(new BorderLayout());
        this.add(labelPanel);
        this.label = new JLabel();
        label.setFont(new Font("Dialog", Font.PLAIN, 18));
        label.setForeground(Color.BLUE);
        labelPanel.add(label, BorderLayout.CENTER);
        setScore(0);
        setTotal(0);
    }

    private void setLabel(int s, int t) {
    	double percent = 0.0;
    	if(t != 0)
    		percent = 100.0*s/t;
        String results = String.format(
                "      Your current score is %d out of %d. That is %.1f%%.", 
                s, t, percent);
        label.setText(results);
    }
    
    public void setScore(int score) {
        this.score = score;
    }
    
    public void incrementScore() {
        this.score = score + 1;
    }
    
    public void incrementTotal() {
        this.total = total + 1;
        if (total > 0) {
            setLabel(score, total);
        }
    }
    
    public void resetScore() {
        this.score = 0;
        this.total = 0;
        setLabel(score,total);
    }

    public void setTotal(int total) {
        if (total > 0) {
            setLabel(score, total);
        }
        this.total = total;
    }
}
